// Guisinger and Singer 2010 IO

// REPLICATION
clear
use "GS2010 IO Rep Data.dta"
summarize
display c(k)
mdesc
tabmiss
codebook cnum
codebook year
gen egdppc_ln = exp(gdppc_ln)
summarize

#delimit;
set more off;
version 10;

* Dependent Variable;
gen inf_ln=ln(inf_cp);
sort cnum year;
by cnum: gen inf_ln_lag1=inf_ln[_n-1];
by cnum: gen inf_ln_lag2=inf_ln[_n-2];
by cnum: gen inf_ln_lag4=inf_ln[_n-4];

*Changed OECD coding to make consistent;
sort cnum year;
by cnum: replace oecd=1 if oecd[_n-1]==1;

* Exchange Rate Regime Categories created as interaction terms;
sort cnum year;
gen dejure_fix=[dejure==1|dejure==2] if dejure~=.;
by cnum: gen dejure_fix_lag=dejure_fix[_n-1];
gen defacto_fix=[defacto_combo==1|defacto_combo==2] if defacto_combo~=. & defacto_combo<5;
by cnum: gen defacto_fix_lag=defacto_fix[_n-1];
gen both_fix=defacto_fix*dejure_fix;
by cnum: gen both_fix_lag=both_fix[_n-1];

* Exchange Rate Regime Categories creates as exclusive groups. Excluded category both floating;
sort cnum year;
gen djfix_only=[dejure_fix==1 & defacto_fix==0] if dejure_fix~=. & defacto_fix~=.;
by cnum: gen djfix_only_lag=djfix_only[_n-1];
gen dffix_only=[dejure_fix==0 & defacto_fix==1] if dejure_fix~=. & defacto_fix~=.;
by cnum: gen dffix_only_lag=dffix_only[_n-1];
gen bfix_only=[dejure_fix==1 & defacto_fix==1] if dejure_fix~=. & defacto_fix~=.;
by cnum: gen bfix_only_lag=bfix_only[_n-1];
gen bfloat_only=[dejure_fix==0 & defacto_fix==0] if dejure_fix~=. & defacto_fix~=.;

#delimit;

* Measure of credibility: 1- Three year (t-1 to t-3) moving average of % caught defecting from policy
sort cnum year;
by cnum: gen credlose_djfix = 1-((djfix_only[_n-3]+ djfix_only[_n-2] + djfix_only[_n-1])/3);

* Other control calculations;
gen cbid_demo=cbi_dum*demo;
gen trade_gdpln=ln(trade_gdp);

gen t70=[year==1970 |year==1971 |year==1972 |year==1973 |year==1974];
gen t75=[year==1975 |year==1976 |year==1977 |year==1978 |year==1979];
gen t80=[year==1980 |year==1981 |year==1982 |year==1983 |year==1984];
gen t85=[year==1985 |year==1986 |year==1987 |year==1988 |year==1989];
gen t90=[year==1990 |year==1991 |year==1992 |year==1993 |year==1994];
gen t95=[year==1995 |year==1996 |year==1997 |year==1998 |year==1999];
gen t00=[year==2000 |year==2001 |year==2002 |year==2003 |year==2004];

gen d70=[year==1970 |year==1971 |year==1972 |year==1973 |year==1974 | year==1975 |year==1976 |year==1977 |year==1978 |year==1979];
gen d80=[year==1980 |year==1981 |year==1982 |year==1983 |year==1984 | year==1985 |year==1986 |year==1987 |year==1988 |year==1989];
gen d90=[year==1990 |year==1991 |year==1992 |year==1993 |year==1994 | year==1995 |year==1996 |year==1997 |year==1998 |year==1999];
gen d00=[year==2000 |year==2001 |year==2002 |year==2003 |year==2004];

xtset cnum year;

summarize;

// Table 1 (H1) All Countries With Fixed Effects, clustered on cnum 

#delimit;
sort cnum year;
xtreg inf_ln L(2).inf_ln dejure_fix_lag defacto_fix_lag  both_fix_lag
cbi_dum demo cbid_demo
any_crisis_lag5
gdpgr gdppc_ln
kaopen trade_gdpln
d80 d90 d00, fe robust cl(cnum);

outreg2 using gs2010_io_long, replace;
codebook cnum if e(sample);
codebook year if e(sample);
predict yhat, xbu;
mean yhat if both_fix_lag == 1;
mean yhat if defacto_fix_lag == 1;

mdesc inf_ln inf_ln_lag2 dejure_fix_lag defacto_fix_lag  both_fix_lag
cbi_dum demo cbid_demo
any_crisis_lag5
gdpgr gdppc_ln
kaopen trade_gdpln
d80 d90 d00;

// Table 2 (H2): Lose Cred with De Jure Fix, Fixed Effects - All Countries

#delimit;
gen idejure_credl_djfix=dejure_fix_lag*credlose_djfix;
gen i_dem_credl_djfix= demo*idejure_credl_djfix;

#delimit;
xtreg inf_ln L(4).inf_ln dejure_fix_lag defacto_fix_lag  
idejure_credl_djfix
cbi_dum demo cbid_demo
any_crisis_lag5
gdpgr gdppc_ln
kaopen trade_gdpln
d80 d90 d00, fe robust cl(cnum);
outreg2 using gs2010_io_long, append excel;
codebook cnum if e(sample);
codebook year if e(sample);

mdesc inf_ln inf_ln_lag4 dejure_fix_lag defacto_fix_lag
idejure_credl_djfix
cbi_dum demo cbid_demo
any_crisis_lag5
gdpgr gdppc_ln
kaopen trade_gdpln
d80 d90 d00; 

//IMPUTATION
// m = 40
clear
use "GS2010 IO Imp Data.dta"
summarize

mi import flong, m(imp) id(year cnum) imp(dejure-i_dem_credl_djfix)

// Table 1 (H1) All Countries With Fixed Effects, clustered on cnum 
mi estimate, esampvaryok post  saving(miest): xtreg inf_ln inf_ln_lag2 dejure_fix_lag defacto_fix_lag  both_fix_lag cbi_dum demo cbid_demo any_crisis_lag5 gdpgr gdppc_ln kaopen trade_gdpln d80 d90 d00, fe robust cl(cnum)

// Table 2 (H2): Lose Cred with De Jure Fix, Fixed Effects - All Countries;
mi estimate, esampvaryok post: xtreg inf_ln inf_ln_lag4 dejure_fix_lag defacto_fix_lag  idejure_credl_djfix cbi_dum demo cbid_demo any_crisis_lag5 gdpgr gdppc_ln kaopen trade_gdpln d80 d90 d00, fe robust cl(cnum)

// m = 5
clear
use "GS2010 IO Imp Data 5.dta"
summarize

mi import flong, m(imp) id(year cnum) imp(dejure-i_dem_credl_djfix)

// Table 1 (H1) All Countries With Fixed Effects, clustered on cnum 
mi estimate, esampvaryok post: xtreg inf_ln inf_ln_lag2 dejure_fix_lag defacto_fix_lag  both_fix_lag cbi_dum demo cbid_demo any_crisis_lag5 gdpgr gdppc_ln kaopen trade_gdpln d80 d90 d00, fe robust cl(cnum)

// Table 2 (H2): Lose Cred with De Jure Fix, Fixed Effects - All Countries;
mi estimate, esampvaryok post: xtreg inf_ln inf_ln_lag4 dejure_fix_lag defacto_fix_lag  idejure_credl_djfix cbi_dum demo cbid_demo any_crisis_lag5 gdpgr gdppc_ln kaopen trade_gdpln d80 d90 d00, fe robust cl(cnum)

// m = 90
clear
use "GS2010 IO Imp Data 90.dta"
summarize

mi import flong, m(imp) id(year cnum) imp(dejure-i_dem_credl_djfix)

// Table 1 (H1) All Countries With Fixed Effects, clustered on cnum 
mi estimate, esampvaryok post: xtreg inf_ln inf_ln_lag2 dejure_fix_lag defacto_fix_lag  both_fix_lag cbi_dum demo cbid_demo any_crisis_lag5 gdpgr gdppc_ln kaopen trade_gdpln d80 d90 d00, fe robust cl(cnum)

// Table 2 (H2): Lose Cred with De Jure Fix, Fixed Effects - All Countries;
mi estimate, esampvaryok post: xtreg inf_ln inf_ln_lag4 dejure_fix_lag defacto_fix_lag  idejure_credl_djfix cbi_dum demo cbid_demo any_crisis_lag5 gdpgr gdppc_ln kaopen trade_gdpln d80 d90 d00, fe robust cl(cnum)

// HD
clear
use "GS2010 IO Imp Data HD.dta"
summarize

mi import flong, m(imp) id(year cnum) imp(dejure-i_dem_credl_djfix)
mi xtset cnum year

// Table 1 (H1) All Countries With Fixed Effects, clustered on cnum 
mi estimate, esampvaryok post: xtreg inf_ln inf_ln_lag2 dejure_fix_lag defacto_fix_lag  both_fix_lag cbi_dum demo cbid_demo any_crisis_lag5 gdpgr gdppc_ln kaopen trade_gdpln d80 d90 d00, fe robust cl(cnum)

// Table 2 (H2): Lose Cred with De Jure Fix, Fixed Effects - All Countries;
mi estimate, esampvaryok post: xtreg inf_ln inf_ln_lag4 dejure_fix_lag defacto_fix_lag  idejure_credl_djfix cbi_dum demo cbid_demo any_crisis_lag5 gdpgr gdppc_ln kaopen trade_gdpln d80 d90 d00, fe robust cl(cnum)

// MICE
clear
use "GS2010 IO Imp Data MICE.dta"
summarize

mi import flong, m(imp) id(year cnum) imp(dejure-i_dem_credl_djfix)
mi xtset cnum year

// Table 1 (H1) All Countries With Fixed Effects, clustered on cnum 
mi estimate, esampvaryok post: xtreg inf_ln inf_ln_lag2 dejure_fix_lag defacto_fix_lag  both_fix_lag cbi_dum demo cbid_demo any_crisis_lag5 gdpgr gdppc_ln kaopen trade_gdpln d80 d90 d00, fe robust cl(cnum)

// Table 2 (H2): Lose Cred with De Jure Fix, Fixed Effects - All Countries;
mi estimate, esampvaryok post: xtreg inf_ln inf_ln_lag4 dejure_fix_lag defacto_fix_lag  idejure_credl_djfix cbi_dum demo cbid_demo any_crisis_lag5 gdpgr gdppc_ln kaopen trade_gdpln d80 d90 d00, fe robust cl(cnum)
